package org.pictual.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.pictual.controller.WikiObtieneDatos;
import org.pictual.util.RequestKeys;

public class WikiApiAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String query = request.getParameter(RequestKeys.QUERY);
		String from = request.getParameter(RequestKeys.FROM);
		String cadena = WikiObtieneDatos.getData(request.getParameter(RequestKeys.QUERY), request.getParameter(RequestKeys.FROM));
		
		request.setAttribute(RequestKeys.CADENA_WIKI, cadena);
		request.setAttribute(RequestKeys.QUERY, query);
		request.setAttribute(RequestKeys.FROM, from);
		
//		FileWriter fichero = null;
//        PrintWriter pw = null;
//        String txt = "pages/txt/"+query+"_"+from+".txt";
//        try
//        {
//        	
//            fichero = new FileWriter(txt);
//            pw = new PrintWriter(fichero);
//
//            pw.println(cadena);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//           try {
//           // Nuevamente aprovechamos el finally para 
//           // asegurarnos que se cierra el fichero.
//           if (null != fichero)
//              fichero.close();
//           } catch (Exception e2) {
//              e2.printStackTrace();
//           }
//        }
//		
//		request.setAttribute(RequestKeys.TXT, txt);
		return mapping.findForward("success");
	}
}
